package cn.lyl.basic.service.impl;


import cn.lyl.basic.mapper.BaseMapper;
import cn.lyl.basic.query.BaseQuery;
import cn.lyl.basic.service.IBaseService;
import cn.lyl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 业务实现类：部门
 */

//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional //默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.findOne(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //1.先查询总数量，计算分页栏totals
        Integer totals = baseMapper.queryCount(query);
            if(totals < 1){//在数据为空的情况下提高响应速度
            return new PageList<>();
            }
        //2.再查询当前页的数据：显示当前页的数据
        //data - 当前页的分页数据 limit
        List<T> data = baseMapper.queryData(query);
        return new PageList<>(data,totals);
    }

    @Override
    public void patchDelete(List<Long> ids) {

        baseMapper.patchDelete(ids);
    }

   
}
